import 'dart:math';

import 'package:flutter/widgets.dart';

class MySliverDelegate extends SliverPersistentHeaderDelegate {
 final double? maxHeight;
 final double? minHeight;
 final Widget? child;
 final bool? forceRefresh; //是否需要强制刷新
  // double? shrinkOffset;
 final Widget Function(BuildContext context, double shrinkOffset, bool overlapsContent, Widget? child)? childBuildHandler;
  Function(double)? callTop;
  MySliverDelegate({
    required this.maxHeight,
    required this.minHeight,
    this.child,
    this.childBuildHandler,
    this.forceRefresh = false,
    this.callTop,
  });

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    if (callTop != null) callTop!(shrinkOffset);
    if (childBuildHandler == null) {
      return SizedBox.expand(
        child: child,
      );
    }
    return childBuildHandler!(context, shrinkOffset, overlapsContent, child);
  }

  @override
  double get maxExtent => max(maxHeight!, minHeight!);

  @override
  double get minExtent => minHeight!;

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
    return (forceRefresh ?? false) ? true : maxHeight != oldDelegate.maxExtent || minHeight != oldDelegate.minExtent;
  }
}
